<template>
	<div class="openClass">
		<div class="u_resourceH classH clear">
			<div class="u_resourceH_l fl">
				<template v-if="!$store.state.isSchool">
					<el-select filterable placeholder="请选择" v-model="subjectId" @change="changeSelect">
						<el-option v-for="item in subjectList" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</template>
			</div>
			<!--  -->
			<div class="u_resourceH_r fr">
				<el-input size="medium" placeholder="试卷名称" suffix-icon="el-icon-search" @change="changSearch" v-model="keyword">
				</el-input>
			</div>
		</div>
		<!--  -->
		<div class="classBox" v-loading="loading">
			<ul>
				<li class="classLs clear" v-for="(item, index) in testList" :key="item.id">
					<span class="classLs_img" to="###">
						<img src="../../../public/images/format/kaoshi.png">
					</span>
					<div class="classLs_info resource_g">
						<div class="classLs_1 clear">
							<a target="_blank" @click="pathURL(item.id)">{{item.res_name}}</a>
							<div class="resource_ioperation fr">
								<el-popover placement="bottom" width="" popper-class="ioperation_hover" trigger="hover">
									<ul class="r_ioperation">
										<!-- <li>取消收藏</li> -->
										<li @click="removeList(item.id)">取消收藏</li>
									</ul>
									<i slot="reference" class="el-icon-arrow-down"></i>
								</el-popover>
							</div>
						</div>
						<div class="classLs_4">
							<span v-if="item.sub_name">{{item.sub_name}}</span>
							<span v-if="item.create_name">出题：{{item.create_name}}</span>
							<span v-if="item.question_num">题量：{{item.question_num}}题</span>
							<span v-if="item.fav_num">收藏：{{item.fav_num}}</span>
						</div>
					</div>
				</li>
			</ul>
		</div>
		<div class="pageBox" v-show="total_number > page_size">
			<el-pagination @current-change="change_page" :page-size="page_size" :current-page="page_index" layout="prev, pager, next" :total="total_number">
			</el-pagination>
		</div>
		<div class="dependent" v-if="placeholder">
			<i class="ipm image_nofound"></i>
			<p>
				还没发现任何相关的内容哦~
			</p>
		</div>
	</div>
</template>
<script>
export default {
	name: '',
	data() {
		return {
			subjectId: 0, //学科
			subjectList: this.$g.subjectList, //学科表
			testList: [],
			page_index: 1,
			page_size: this.$g.page_size,
			total_number: 0,
			question_type: '',
			keyword: '', //资源名称
			placeholder: false,
			loading: true
		}
	},
	created() {
		this.getTestList()
	},
	methods: {
		pathURL(id) {
			let host = sessionStorage.getItem('host')
			let redirectParams = sessionStorage.getItem('redirect_params')
			let link = `${this.BASE_URL}${redirectParams}&host=${host}&routerUrl=/test/details&id=${id}`
			window.open(link)
		},
		getTestList() {
			let success = (res => {
				let data = res.data;

				this.testList = data.data;
				this.total_number = data.data_sum;
				this.page_index = parseInt(data.page_index)
				this.loading = false;
				this.subjectList = this.$g.subjectList
				this.placeholder = this.testList.length === 0 ? true : false
			});
			this.$ajax({
				url: '/api/favourite/favourite',
				method: 'get',
				params: {
					keyword: this.keyword,
					page: this.page_index,
					size: this.page_size,
					user_id: this.$g.user_id,
					origin: 4,
					data_type: 2,
					subject_id: this.subjectId
				},
				func: { success: success }
			})
		},
		//搜索
		changSearch() {
			this.page_index = 1
			this.getTestList();
		},
		//筛选
		changeSelect(val) {
			this.page_index = 1
			this.getTestList();
		},
		//分页
		change_page(val) {
			this.page_index = val;
			this.getTestList()
		},
		//删除
		removeList(res_id) {
			let success = (res => {
				this.$message.success(res.msg)
				this.getQuestion()
			})
			this.$confirm('此操作是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				customClass: 'confirm-them',
			}).then(() => {
				let success = (res => {
					this.$message.success(res.msg)
					this.getTestList()
				})
				this.$ajax({
					url: '/api/favourite/favourite',
					method: 'delete',
					params: {
						user_id: this.$g.user_id,
						origin: 4,
						res_id: res_id
					},
					func: { success: success }
				})
			}).catch(() => {});
		},
	}
}
</script>